package com.qian.service.ddj.register.service;

import com.alibaba.fastjson.JSON;
import com.qian.api.ddj.register.model.ShRegister;
import com.qian.api.ddj.register.service.IShRegisterService;
import com.qian.api.ddj.util.SignUtils;
import com.qian.service.ddj.entity.DdjBusinessGroup;
import com.qian.service.ddj.register.dao.ShRegisterRepository;
import com.qian.service.ddj.register.entity.DdjShRegister;
import com.yaowk.api.common.BaseException;
import com.yaowk.api.utils.HttpUtils;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

@Service
@Slf4j
public class ShRegisterServiceImpl extends BaseServiceImpl<ShRegister, DdjShRegister, Integer> implements IShRegisterService {

    @Autowired
    private ShRegisterRepository dao;

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected DdjShRegister model2Entity(ShRegister model) {
        DdjShRegister register = null;
        if (isNotEmpty(model)) {
            register = new DdjShRegister();
            register.setAcctBankName(model.getAcctBankName());
            register.setAcctName(model.getAcctName());
            register.setAcctNo(model.getAcctNo());
            register.setAcctType(model.getAcctType());
            register.setCorporationCertno(model.getCorporationCertno());
            register.setCorporationName(model.getCorporationName());
            register.setIdNumber(model.getIdNumber());
            register.setManageId(model.getManageId());
            register.setManageType(model.getManageType());
            register.setMchtName(model.getMchtName());
            register.setMchtPhone(model.getMchtPhone());
            register.setMerAddr(model.getMerAddr());
            register.setMerMark(model.getMerMark());
            register.setName(model.getName());
            register.setPhone(model.getPhone());
            register.setSettacctBankno(model.getSettacctBankno());
            register.setUserId(model.getUserId());
            register.setSubMchId(model.getSubMchId());
            DdjBusinessGroup ddjBusinessGroup = new DdjBusinessGroup();
            ddjBusinessGroup.setId(model.getGroupId());
            register.setBusinessGroup(ddjBusinessGroup);
        }
        return register;
    }

    @Override
    protected ShRegister entity2model(DdjShRegister entity) {
        ShRegister register = null;
        if (isNotEmpty(entity)) {
            register = new ShRegister();
            register.setAcctBankName(entity.getAcctBankName());
            register.setAcctName(entity.getAcctName());
            register.setAcctNo(entity.getAcctNo());
            register.setAcctType(entity.getAcctType());
            register.setCorporationCertno(entity.getCorporationCertno());
            register.setCorporationName(entity.getCorporationName());
            register.setIdNumber(entity.getIdNumber());
            register.setManageId(entity.getManageId());
            register.setManageType(entity.getManageType());
            register.setMchtName(entity.getMchtName());
            register.setMchtPhone(entity.getMchtPhone());
            register.setMerAddr(entity.getMerAddr());
            register.setMerMark(entity.getMerMark());
            register.setName(entity.getName());
            register.setPhone(entity.getPhone());
            register.setSettacctBankno(entity.getSettacctBankno());
            register.setUserId(entity.getUserId());
            register.setSubMchId(entity.getSubMchId());
        }
        return register;
    }

    @Override
    public ShRegister save(ShRegister model) {
        Map<String, String> map = new TreeMap<>();
        String txnTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        map.put("name", model.getName());
        map.put("phone", model.getPhone());
        map.put("mchtName", model.getMchtName());
        map.put("mchtPhone", model.getMchtPhone()); // 客服电话
        map.put("manageType", model.getManageType());
        map.put("manageId", model.getManageId());
        map.put("merMark", model.getMerMark());
        map.put("merAddr", model.getMerAddr());
        map.put("corporationName", model.getCorporationName());
        map.put("corporationCertNo", model.getCorporationCertno());
        map.put("acctType", model.getAcctType());
        map.put("acctName", model.getAcctName());
        map.put("IdNumber", model.getIdNumber());
        map.put("acctBankName", model.getAcctBankName());
        map.put("acctNo", model.getAcctNo());
        map.put("settacct_bankno", model.getSettacctBankno());
        map.put("userid", model.getUserId());
        map.put("bussauthnum", model.getBussAuthNum());
        map.put("province_code", model.getProvinceCode());
        map.put("city_code", model.getCityCode());
        map.put("district_code", model.getDistrictCode());
        /** 参与验签的字段 */
        String sign = SignUtils.params(map);
        sign = DigestUtils.md5Hex(sign + model.getKey());
        /** 上送的值为 */
        map.put("sign", sign);
        map.put("pay_number", txnTime);
        map.put("orderCode", "kl_Register");
        String baowen = SignUtils.params(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/newWechats/newWeChatpayment_mobile.action", baowen);
        log.info(sr);
        Map<String, String> result = JSON.parseObject(sr, Map.class);
        if (!"000000".equals(result.get("respCode"))) {
            throw new BaseException(result.get("respInfo"));
        }
        model.setSubMchId(result.get("subMchId"));
//        model.setSubMchId("subMchId");
        dao.save(model2Entity(model));
        return model;
    }
}
